package org.example.tcp.server;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Scanner;

public class TcpServer {
    public static HashMap<String, Socket> map = new HashMap<>();
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务器启动成功,等待连接.........");
        while(true) {
            Socket socket = serverSocket.accept();
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len = inputStream.read(bytes);
            String name = new String(bytes, 0, len).split(":")[0];
            InetAddress inetAddress = socket.getInetAddress();
            int port = socket.getPort();
            System.out.println("连接到"+inetAddress+"/"+port+"---"+name);
            map.put(name,socket);
            MyRunable myRunable = new MyRunable(socket);
            Thread thread = new Thread(myRunable);
            thread.start();
        }
    }
}
